package Modules.CommunicationsModule
{
	import Modules.MainTimeLine;
	
	import com.adobe.serialization.json.*;
//	import flash.net.*;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
//	import flash.net.URLVariables;
	
	public class BulletinCommuncationModule
	{
		public function BulletinCommuncationModule()
		{
		}
		
		// get bulletin faq from db
		public static function getBulletinFAQ(){
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			
			var loadFAQRequest:URLRequest = new URLRequest(MainTimeLine.rootURL + "PHP/bulletin/get_BulletinFAQ.php");
			loadFAQRequest.method = URLRequestMethod.POST;
			
			//attempt loading of th bulletin
			try{
				loader.load(loadFAQRequest);
				loader.addEventListener(Event.COMPLETE, function(event:Event):void{completeGetBulletinFAQ(loader);});
			} catch(error:IOError) {
				dump("unable to load bulletin faq");	
			}
		}
		
		// complete get bulletin faq from db
		public static function completeGetBulletinFAQ(loader:URLLoader):void{
			
			var loadedArray:Array = [];
			var decodedArray:Array = [];
			var incoming:String = loader.data;
			
			// have questions?
			if( incoming != "no questions" ){
				
				loadedArray = incoming.split("|");
				
				var length:int = loadedArray.length-1;
				
				for(var i:int=0; i<length; i++){
					
					if(loadedArray[i] != null){
						decodedArray.push(JSON.decode(loadedArray[i]));
					}
				}
				
				MainTimeLine.session.player.bulletinFAQ.setBulletinFAQ(decodedArray);
			}
			
			MainTimeLine.loadedBulletinFAQ = true;
		}
		
		// output error to ui
		public static function dump(message:String):void{
			MainTimeLine.debugText.text = message;
		}
	}
}